﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NWBA_Public
{
    public partial class MyStatements : System.Web.UI.Page
    {
        private static List<Account> myAcct;
        protected void Page_Load(object sender, EventArgs e)
        {
            //see weather the user has logged in or not
            if ((string)(Session["userName"]) == null)
            {
                //if not logged in direct him to login.aspx
                Response.Redirect("Login.aspx", true);
            }
            if (!IsPostBack)
            {
                //retrieve all the account that associate with this user
                myAcct = AccountRepostiory.Select(Session["Customer"] as Customer);
                //associate all the accounts to the dropdownlist for user to select it.
                if (this.dropdownlistAccounts.Items.Count <= myAcct.Count)
                {
                    foreach (Account acct in myAcct)
                    {
                        this.dropdownlistAccounts.Items.Add(acct.AccountNumber.ToString());
                    }
                }
                // Charge service fee
                TransactionRepository.ServiceFee(myAcct.Where(a => a.AccountNumber.ToString() == dropdownlistAccounts.SelectedValue).First() as Account, "My statement service fee", false);
            }
            updateBalance(Convert.ToInt32(dropdownlistAccounts.SelectedValue));
        }

        protected void dropdownlistAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridviewTransactions.DataBind();
            updateBalance(Convert.ToInt32(dropdownlistAccounts.SelectedValue));
        }

        protected void updateBalance(int accountID)
        {
            Account account = AccountRepostiory.SelectSingleAccount(accountID);
            lblBalance.Text = account.Balance.ToString();
        }
    }
}